package com.pjs.tdd;

import static com.pjs.tdd.CurrentAccount.currentAccount;
import static com.pjs.tdd.DataProvider.pounds;
import static com.pjs.tdd.Money.money;
import static com.pjs.tdd.NegativeTransaction.negativeTransaction;
import static com.pjs.tdd.PositiveTransaction.positiveTransaction;
import static com.pjs.tdd.SimpleAccountIdentifier.simpleAccountIdentifier;
import static org.junit.Assert.assertEquals;

import org.junit.Test;

import com.pjs.test.framework.AbstractUnitTestCase;

public class AccountTest extends AbstractUnitTestCase<Account> {

	public AccountTest() {
		super(Account.class);
	}
	
	@Test
	public void accountHasRequiredType(){
		classUnderTest=makeAccount(1000);
		assertEquals("account should be a current account", classUnderTest.type(),AccountType.current);
	}
	
	@Test
	public void processMutlipleNegativeTransactionsOnAccount(){
		classUnderTest=makeAccount(1000);
		classUnderTest.processTransaction(negativeTransaction(pounds(500)),negativeTransaction(pounds(500)));
		assertAccount(0,Currency.GBP,classUnderTest);
	}
	
	@Test
	public void processMutliplePositiveTransactionsOnAccount(){
		classUnderTest=makeAccount(0);
		classUnderTest.processTransaction(positiveTransaction(pounds(500)),positiveTransaction(pounds(500)));
		assertAccount(1000,Currency.GBP,classUnderTest);
	}
	
	@Test
	public void rollbackMutlipleTransactionsOnAccount(){
		classUnderTest=makeAccount(1000);
		classUnderTest.rollbackTransaction(positiveTransaction(pounds(500)),positiveTransaction(pounds(500)));
		assertAccount(0,Currency.GBP,classUnderTest);
		assertAccountTransactions(2);
		
	}
	
	private static Account makeAccount(int amount){
		return currentAccount(simpleAccountIdentifier(1, AccountType.current),pounds(amount));
	}
	
	private void assertAccount(double balance, Currency currency, Account account){
		assertEquals(money(balance,currency),account.balance());
		
	}
	
	private void assertAccountTransactions(int expectedTransactions){
		assertEquals(expectedTransactions,classUnderTest.transactionsProcessed());
		
	}

}
